package com.pat.java.base.socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

public class ReadClient {
	public static void main(String args[]) throws Exception {
		Socket clientSocket = null ;
		DataOutputStream outbound = null ;
		DataInputStream inbound = null ;
		InputStreamReader inS = null ;
		try {
			//与服务器建立连接
			clientSocket = new Socket("127.0.0.1", 12000);
			System.out.println("Client1: " + clientSocket);
			//初始化流对象
			outbound = new DataOutputStream(
					clientSocket.getOutputStream());
			inbound = new DataInputStream(
					clientSocket.getInputStream());
			inS = new InputStreamReader(inbound);
			//发送数据
			outbound.writeBytes("hello\r\n");

			System.out.println("hello");
			outbound.flush();
			int c;
			while ((c = inS.read()) != -1) {
				System.out.print((char) c);
			}
		} catch (UnknownHostException uhe) {
			System.out.println("UnknownHostException: " + uhe);
		}

		catch (IOException ioe) {
			System.err.println("IOException: " + ioe);
		} finally {
			//关闭流，及clientSocket
			inS.close();
			outbound.close();
			inbound.close();
			clientSocket.close();
		}
	}
}